<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/43">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Apache/42. apache 访问控制/">apache 访问控制</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<h2 id="allow-和-deny-规则"><a href="#allow-和-deny-规则" class="headerlink" title="allow 和 deny 规则"></a>allow 和 deny 规则</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先举例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Order deny,allow</div><div class="line">deny from all</div><div class="line">allow from 127.0.0.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们判断的依据是这样的：</p>
<ol>
<li>看 Order 后面的，哪个在前，哪个在后</li>
<li>如果 deny 在前，那么就需要看 deny from 这句，然后看 allow from 这一句</li>
<li>规则是一条一条的匹配的，不管是 deny 在前还是 allow 在前，都是会生效的。比如例子中，先 deny 了所有，然后又 allow 了127.0.0.1，所以 127.0.0.1 是通过的。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Order allow,deny</div><div class="line">Deny from all</div><div class="line">Allow from 127.0.0.1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个就会 deny 所有了， 127.0.0.1也会被 deny 。因为顺序是先 allow 然后 deny ，虽然一开始 allow 了127.0.0.1，但是后面又拒绝了它。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Order allow,deny</div><div class="line">Deny from all</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的规则就表示，全部都不能通</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Order deny,allow</div><div class="line">Deny from all</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面的规则就表示，全部都不能通</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Order deny,allow</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只有顺序，没有具体的规则，表示，全部都可以通行（默认的），因为 allow 在最后。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Order allow,deny</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个表示，全部都不能通行（默认的），因为 deny 在最后。</p>
<ol>
<li>做某个目录闲置，只允许内网 ip 访问，这个目录可以是网站根目录，也就是整个站点都要做限制了。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">&lt;Directory <span class="string">"/data/www、"</span>&gt;</div><div class="line">    AllowOverride None</div><div class="line">    Options None</div><div class="line">    Order deny,allow</div><div class="line">    Deny from all</div><div class="line">    Allow from 127.0.0.1</div><div class="line">&lt;/Directory&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：只允许 127.0.0.1 访问，其他 ip 全部拒绝。</p>
<ol>
<li>针对请求的 uri 去限制，前面安装的 discuz 论坛，访问后台是 admin.php，那么就可以针对这个 admin.php 做限制。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;filesmatch <span class="string">"(.*)admin(.*)"</span>&gt;</div><div class="line">    Order Deny,Allow</div><div class="line">    Deny from all</div><div class="line">    Allow from 127.0.0.1</div><div class="line">&lt;/filesmatch&gt;</div><div class="line">`</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：这里用到了 filesmatch 语法，表示匹配的意思。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测是否有错，重启apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用curl 测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># curl -x192.168.0.99:80 -I www.test.com/admin.php</span></div><div class="line">HTTP/1.1 403 Forbidden</div><div class="line">Date: Thu, 05 Jan 2017 10:37:38 GMT</div><div class="line">Server: Apache/2.2.31 (Unix) PHP/5.6.6</div><div class="line">Content-Type: text/html; charset=iso-8859-1</div><div class="line"> </div><div class="line">[root@lamp ~]<span class="comment"># curl -x127.0.0.1:80 -I www.test.com/admin.php </span></div><div class="line">HTTP/1.1 200 OK</div><div class="line">Date: Thu, 05 Jan 2017 10:37:40 GMT</div><div class="line">Server: Apache/2.2.31 (Unix) PHP/5.6.6</div><div class="line">X-Powered-By: PHP/5.6.6</div><div class="line">Set-Cookie: cbq6_2132_saltkey=W35308HL; expires=Sat, 04-Feb-2017 10:37:40 GMT; Max-Age=2592000; path=/; httponly</div><div class="line">Set-Cookie: cbq6_2132_lastvisit=1483609060; expires=Sat, 04-Feb-2017 10:37:40 GMT; Max-Age=2592000; path=/</div><div class="line">Set-Cookie: cbq6_2132_sid=PUbGdd; expires=Fri, 06-Jan-2017 10:37:40 GMT; Max-Age=86400; path=/</div><div class="line">Set-Cookie: cbq6_2132_lastact=1483612660%09admin.php%09; expires=Fri, 06-Jan-2017 10:37:40 GMT; Max-Age=86400; path=/</div><div class="line">Cache-Control: max-age=0</div><div class="line">Expires: Thu, 05 Jan 2017 10:37:40 GMT</div><div class="line">Content-Type: text/html; charset=gbk</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本机 ip 192.168.0.99 被拒绝，而 127.0.0.1 可以访问。</p>
<h2 id="Apache设置禁止访问-txt文件"><a href="#Apache设置禁止访问-txt文件" class="headerlink" title="Apache设置禁止访问.txt文件"></a>Apache设置禁止访问.txt文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;做了目录禁止浏览后，目录下面的txt文件还是可以显示里面的内容的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Options -Indexes   FollowSymLinks</div><div class="line">  AllowOverride All</div><div class="line">  Order allow,deny</div><div class="line">  Deny from all</div></pre></td></tr></table></figure>
<h2 id="apache-禁止trace或track防止xss攻击"><a href="#apache-禁止trace或track防止xss攻击" class="headerlink" title="apache 禁止trace或track防止xss攻击"></a>apache 禁止trace或track防止xss攻击</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;TRACE和TRACK是用来调试web服务器连接的HTTP方式。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;支持该方式的服务器存在跨站脚本漏洞，通常在描述各种浏览器缺陷的时候，把”Cross-Site-Tracing”简称为XST。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;攻击者可以利用此漏洞欺骗合法用户并得到他们的私人信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;禁用trace可以使用rewrite功能来实现</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_rewrite.c&gt;</div><div class="line">  RewriteEngine On</div><div class="line">  RewriteCondi %&#123;REQUEST_METHOD&#125; ^TRACE</div><div class="line">  RewriteRule .* - [F]</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者还可以直接在apache的配置文件中配置相应参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">TraceEnable off</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/41. apache配置防盗链/">apache配置防盗链</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果站点是一个图片站，有很多非常漂亮的美女图片，时间久了会有很多人来网站借图片，有的直接下载走了，还有的直接取走图片的地址，那么他就可以直接把图片地址放到他自己的网站上，他的用户可以直接从他的网站上查看这个图片，而实际上浏览这个图片是从我的网站上访问的。这样，这个图片所产生的宽带开销对我来说没有任何意义，毕竟看图片的人不是我的客户，而是人家的。所以我们要把这些图片限制一下，凡是在第三方站点上，严禁访问我站点的图片。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">SetEnvIfNoCase Referer <span class="string">"^http://.*\.test\.com"</span> local_ref</div><div class="line">SetEnvIfNoCase Referer <span class="string">".*\.aaa\.com"</span> local_ref</div><div class="line">SetEnvIfNoCase Referer <span class="string">"^$"</span> local_ref</div><div class="line">&lt;filesmatch <span class="string">"\.(txt|doc|mp3|zip|rar|jpg|gif|png|js|css)"</span>&gt;</div><div class="line">  Order Allow,Deny</div><div class="line">  Allow from env=local_ref</div><div class="line">&lt;/filesmatch&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测是否有错，并重启apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：在这段配置中涉及到一个名词 referer ，它其实就是上次访问的网站连接。我们是根据来源链接做限制的，如果来源链接并不是想要的，就直接拒绝，这就是防盗链的原理。当然不止是图片，mp3，rar，zip等文件同样支持。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/40. 什么是etag工作原理及配置/">什么是etag工作原理及配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Etag 是URL的Entity Tag，用于标示URL对象是否改变，区分不同语言和Session等等。具体内部含义是使服务器控制的，就像Cookie那样。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HTTP协议规格说明定义ETag为“被请求变量的实体值” 。另一种说法是，ETag是一个可以与Web资源关联的记号（token）。典型的Web资源可以一个Web页，但也可能是JSON或XML文档。服务器单独负责判断记号是什么及其含义，并在HTTP响应头中将其传送到客户端。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Etag在HTTP1.1中有介绍，主要的作用就是在(css file, image, javascript file)文件后面添加一个唯一的参数（相当于查询参数字符串），Etag有服务器端生成，并且随着文件的改变而改变，这样浏览器端就会只重新请求获取 Etag发生变化的文件，减少浏览器端数据的流量，加快浏览器的反应速度，重要的是减轻服务器端的压力，所以服务器端Etag的实现就比较重要了。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在我们有个问题为什么要使用Etag呢？ </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Etag主要为了解决Last-Modified无法解决的一些问题.他能比Last_Modified更加精确的知道文件是否被修改过.如果有个 文件修改非常频繁，比如在秒以下的时间内进行修改，比如1秒内修改了10次，If-Modified-Since能检查只能秒级的修改，所以这种修改无法 判断.原因是UNIX记录MTIME只能精确到秒.所以我们选择生成Etag，因为Etag可以综合Inode，MTime和Size，可以避免这个问题.</p>
<h2 id="Etag的工作原理"><a href="#Etag的工作原理" class="headerlink" title="Etag的工作原理"></a>Etag的工作原理</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Etag在服务器上生成后，客户端通过If-Match或者说If-None-Match这个条件判断请求来验证资源是否修改。我们常见的是使用If-None-Match.请求一个文件的流程可能如下： </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;新的请求客户端发起HTTP GET请求一个文件(css ,image,js)；服务器处理请求，返回文件内容和一堆Header(包括Etag,例如”2e681a-6-5d044840″),http头状态码为为200.同一个用户第二次这个文件的请求 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端在一次发起HTTP GET请求一个文件，注意这个时候客户端同时发送一个If-None-Match头，这个头中会包括上次这个文件的Etag(例如”2e681a- 6-5d044840″),这时服务器判断发送过来的Etag和自己计算出来的Etag，因此If-None-Match为False，不返回200，返 回304，客户端继续使用本地缓存； </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：服务器又设置了Cache-Control:max-age和Expires时,会同时使用，也就是说在完全匹配If-Modified-Since和If-None-Match即检查完修改时间和Etag之后，服务器才能返回304.</p>
<h2 id="在Apache中的Etag的配置"><a href="#在Apache中的Etag的配置" class="headerlink" title="在Apache中的Etag的配置"></a>在Apache中的Etag的配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Apache中设置Etag的支持比较简单，只需要在apache的配置中加入下面的内容就可以了： </p>
<ul>
<li>FileETag MTime Size 注解:FileETag指令配置了当文档是基于一个文件时用以创建ETag(实体标签)应答头的文件的属性(ETag的值用于进行缓冲管理以节约网 络带宽)。ETag的值由文件的inode(索引节点)、大小、最后修改时间决定。FileETag指令可以让您选择(如果您想进行选择)这其中 哪些要素 将被使用。主要关键字如下： </li>
<li>INode 文件的索引节点(inode)数 </li>
<li>MTime 文件的最后修改日期及时间 </li>
<li>Size  文件的字节数 </li>
<li>All 所有存在的域，等价于：FileETag INode MTime Size </li>
<li>None 如果一个文档是基于文件的，则不在应答中包含任何ETag头 </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在大型多WEB集群时,使用ETag时有问题,所以有人建议使用WEB集群时不要使用ETag,其实很好解决,因为多服务器时,INode不一样, 所以不同的服务器生成的ETag不一样,所以用户有可能重复下载(这时ETag就会不准),明白了上面的原理和设置后,解决方法也很容易,让ETag后面 二个参数,MTime和Size就好了.只要ETag的计算没有INode参于计算,就会很准了.</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/39. apache 设置图片等静态文件的过期时间/">apache 设置图片等静态文件的过期时间</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;httpd.conf 中加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">ExpiresActive on</div><div class="line">ExpiresByType image/gif  <span class="string">"access plus 1 days"</span></div><div class="line">ExpiresByType image/jpeg <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType image/png <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType text/css <span class="string">"now plus 2 hour"</span></div><div class="line">ExpiresByType application/x-javascript <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresByType application/x-shockwave-flash <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresDefault <span class="string">"now plus 0 min"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释： </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Expires 语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ExpiresByType <span class="built_in">type</span>/encoding <span class="string">"&lt;base&gt; [plus] &#123;&lt;num&gt; &lt;type&gt;&#125;*"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中<base>是下列之一：</p>
<ul>
<li>access</li>
<li>now (等价于’access’)</li>
<li>modification</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;plus关键字是可选的。<num>必须是整数[可以被atoi()接受的]，<type>是下列之一：</type></num></p>
<ul>
<li>years</li>
<li>months</li>
<li>weeks</li>
<li>days</li>
<li>hours</li>
<li>minutes</li>
<li>seconds</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考： <a href="http://apache.chinahtml.com/mod/mod_expires.html" target="_blank" rel="external">http://apache.chinahtml.com/mod/mod_expires.html</a></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/4. apache 的默认编码/">apache 的默认编码</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在httpd.conf 中有一个参数是控制apache的默认编码的，<code>AddDefaultCharset</code>  它的作用就是当开发者没有指定程序的编码时，apache会按照这里的配置来显示效果。一般这个参数是不需要的，因为很少有程序不指定编码。为了引起不必要的麻烦，建议开启这个编码的童鞋去掉吧。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/38. Linux Apache安装加载mod_headers模块/">Linux Apache安装加载mod_headers模块</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;mod_headers模块用于控制和修改HTTP请求头和应答头</p>
<ol>
<li>进入到mod_headers.c目录</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/httpd-2.2.24/modules/metadata</div></pre></td></tr></table></figure>
<ol>
<li>执行编译加载程序</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/apache2/bin/apxs -i -a -c mod_headers.c</div></pre></td></tr></table></figure>
<ol>
<li>重新启动httpd程序</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service httpd restart</div></pre></td></tr></table></figure>
<ol>
<li>查看是否已经加载成功</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">httpd -M</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/36. apache 不记录指定文件类型日志/">apache 不记录指定文件类型日志</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果一个站点访问量特别大，那么访问日志就会很多。但有一些访问日志其实可以忽略掉的，比如网站的一些图片，还有 js，css 等静态对象。而这些文件的访问往往是巨量的，而且即使记录这些日志也没有什么用，如何忽略掉这些访问日志呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相关配置为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line"> SetEnvIf Request_URI \.jpg$ image-request</div><div class="line"> SetEnvIf Request_URI \.JPG$ image-request</div><div class="line"> SetEnvIf Request_URI \.png$ image-request</div><div class="line"> SetEnvIf Request_URI \.js$ image-request</div><div class="line"> SetEnvIf Request_URI \.bmp$ image-request</div><div class="line"> SetEnvIf Request_URI \.css$ image-request</div><div class="line"> SetEnvIf Request_URI \.mid$ image-request</div><div class="line"> SetEnvIf Request_URI \.swf$ image-request</div><div class="line"> SetEnvIf Request_URI \.mmf$ image-request</div><div class="line"> SetEnvIf Request_URI \.wma$ image-request</div><div class="line"> SetEnvIf Request_URI \.midi$ image-request</div><div class="line"> ErrorLog <span class="string">"|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-error_%Y%m%d_log 86400"</span></div><div class="line"> CustomLog <span class="string">"|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400"</span> combined env=!image-request</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：在原来日志配置的基础上，增加了一些 image-request 的定义，比如把 gif 、jpg、bpm、swf、js、css 等结尾的全标记为 image-request 。谈后在配置日志的时候加一个标记 env=!image-request ，这里有个叹号，表示取反，这样就可以把这些忽略了。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/37. apache 配置静态缓存/">apache 配置静态缓存</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的静态文件指的是图片、js、css等文件，用户访问一个站点，其实大多数元素都是图片、js、css等，这些静态文件其实是会被客户端的浏览器缓存到本地电脑上的，目的就是为了下次再请求时不再去服务器上下载，这样就加快了速度，提高了用户体验。但这些静态文件总不能一直缓存，它总有一些时效性。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div><div class="line"></div><div class="line"></div><div class="line">&lt;IfModule mod_expires.c&gt;</div><div class="line">ExpiresActive on</div><div class="line">ExpiresByType image/gif <span class="string">"access plus 1 days"</span></div><div class="line">ExpiresByType image/jpeg <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType image/png <span class="string">"access plus 24 hours"</span></div><div class="line">ExpiresByType text/css <span class="string">"now plus 2 hour"</span></div><div class="line">ExpiresByType application/x-javascript <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresByType application/javascript <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresByType application/x-shockwave-flash <span class="string">"now plus 2 hours"</span></div><div class="line">ExpiresDefault <span class="string">"now plus 0 min"</span></div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试配置是否成功，并重启apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl graceful</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用curl 测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># curl -x127.0.0.1:80 'http://www.test.com/static/image/common/logo.png' -IHTTP/1.1 200 OK</span></div><div class="line">Date: Thu, 05 Jan 2017 06:33:18 GMT</div><div class="line">Server: Apache/2.2.31 (Unix) PHP/5.6.6</div><div class="line">Last-Modified: Tue, 31 May 2016 03:08:36 GMT</div><div class="line">ETag: <span class="string">"2854b-1149-5341ab0597500"</span></div><div class="line">Accept-Ranges: bytes</div><div class="line">Content-Length: 4425</div><div class="line">Cache-Control: max-age=86400</div><div class="line">Expires: Fri, 06 Jan 2017 06:33:18 GMT</div><div class="line">Content-Type: image/png</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者使用mod_headers 模块实现</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;IfModule mod_headers.c&gt;</div><div class="line"><span class="comment">#htm,html,txt 类的文件缓存一小时</span></div><div class="line">&lt;filesmatch <span class="string">"\.(html|htm|txt)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=3600"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line"><span class="comment">#css,js,swf 类的文件缓存一个星期</span></div><div class="line">&lt;filesmatch <span class="string">"\.(cssl|js|swf)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=604800"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line"><span class="comment">#jpg,gif,jpeg,png,ico,flv,pdf 类的文件缓存一年</span></div><div class="line">&lt;filesmatch <span class="string">"\.(jpg|gif|jpeg|png|ico|flv|pdf)$"</span>&gt;</div><div class="line">header <span class="built_in">set</span> cache-control <span class="string">"max-age=29030400"</span></div><div class="line">&lt;/filesmatch&gt;</div><div class="line">&lt;/IfModule&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：这里的时间单位可以是days，hours，甚至是min，两种不同的方法，要想使用这些模块，必须要事先已经支持。查看是否支持命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/apache2/bin/apachectl -M</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/35. 使apache的日志文件里不记录图片文件/">使apache的日志文件里不记录图片文件</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">LogFormat <span class="string">"%h %l %u %t \"%r\" %&gt;s %b \"%&#123;Referer&#125;i\" \"%&#123;User-Agent&#125;i\""</span> combined</div><div class="line">LogFormat <span class="string">"%h %l %u %t \"%r\" %&gt;s %b"</span> common</div><div class="line">LogFormat <span class="string">"%&#123;Referer&#125;i -&gt; %U"</span> referer</div><div class="line">LogFormat <span class="string">"%&#123;User-agent&#125;i"</span> agent</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再后面加上图片文件的类型</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">13SetEnvIf Request_URI \.gif$ gif-image</div><div class="line">SetEnvIf Request_URI \.GIF$ gif-image</div><div class="line">SetEnvIf Request_URI \.jpg$ gif-image</div><div class="line">SetEnvIf Request_URI \.JPG$ gif-image</div><div class="line">SetEnvIf Request_URI \.png$ gif-image</div><div class="line">SetEnvIf Request_URI \.js$ gif-image</div><div class="line">SetEnvIf Request_URI \.bmp$ gif-image</div><div class="line">SetEnvIf Request_URI \.css$ gif-image</div><div class="line">SetEnvIf Request_URI \.mid$ gif-image</div><div class="line">SetEnvIf Request_URI \.swf$ gif-image</div><div class="line">SetEnvIf Request_URI \.mmf$ gif-image</div><div class="line">SetEnvIf Request_URI \.wma$ gif-image</div><div class="line">SetEnvIf Request_URI \.midi$ gif-image</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;记日志时</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CustomLog /usr/<span class="built_in">local</span>/apache/logs/access.log combined env=!gif-image</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/34. 让apache记录一个页面的访问时间/">让apache记录一个页面的访问时间</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候，需要我们通过访问日志来观察是哪个页面访问慢，从而可以了解我们网站的某个程序是不是存在问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑httpd.conf </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">LogFormat <span class="string">"%h %l %u %t \"%r\" %&gt;s %b \"%&#123;Referer&#125;i\" \"%&#123;User-Agent&#125;i\""</span> combined</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;改成</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">LogFormat <span class="string">"%h %l %u %t \"%r\" %&gt;s %b \"%&#123;Referer&#125;i\" \"%&#123;User-Agent&#125;i\" %D"</span> combined</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的 %D就是记录页面执行时间的参数，单位是毫秒。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果，想使用s为单位的话，那把%D替换为%T</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启apahce即可生效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考文档：<a href="http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_log_config.html" target="_blank" rel="external">http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_log_config.html</a></p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/45">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
